const mysql = require('mysql2/promise');
const config = require('../config/mysql.js')
const pool = mysql.createPool(config)
exports.pool = pool;

exports.add = async (table, data) => {
    let cols = Object.keys(data)
    let vals = Object.values(data)
    let sql = `insert into ${table}(${cols.join()}) values(${cols.map(item => '?').join()})`
    return await pool.query(sql, vals)
}

exports.edit = async (table, data, where) => {
    let sql = `update ${table} set ${Object.keys(data).map(item => item + '=?').join()} where ${Object.keys(where).map(item => item + '=?').join(' and ')};`
    let vals = Object.values(data).concat(Object.values(where))
    return await pool.query(sql, vals)
}
